<?php

class ArticlesController extends AppController {
  //public $scaffold;
  public $helpers = array('Html', 'TWSForm', 'Paginator');
  public $components = array('Session', 'Util', 'Upload');
  public $name = 'Articles';
  public $paginate;

  function __construct($request=null, $response=null) {
    parent::__construct($request, $response);
    $this->paginate = array(
        'limit' => Configure::read('App.pageResultLimit'),
        'order' => array(
            'Article.created' => 'desc'
            ));
  }

  function index() {
    $this->set('title_for_layout','Caribbean wedding information');
    $this->set("meta_keywords","Articles Isle Vow, Trinidad and Tobago");
    $this->set("meta_description","List of articles pertaining to wedding and event planning in Trinidad and Tobago.");
    $this->set('articles', $this->paginate('Article'));
  }


//admin and public version
  function admin_index() {
    $this->layout = "admin";
    $this->set('articles', $this->paginate('Article'));
  }

  //public version
  function view($id=null) {
    $this->Article->id = $id;
    $this->set('articles', $this->Article->read());
    //TO-DO set the view here to the same as viewing all the announcements
  }

  //admin
  function admin_add() {
    $this->layout = "admin";
    $test = $this->Article->validate;
    if ($this->request->is('post')) {
      $this->_processAction($this->request, 'add');
    }
  }

  //admin
  function admin_edit($id = null) {
    $this->layout = "admin";
    $this->Article->id = $id;
    if ($this->request->is('get')) {
      $this->request->data = $this->Article->read();
    } else {
      //check for delete article
      if (isset($this->request->data['deleteBtn'])) {
        $this->admin_delete($id);
      } else {
        $this->_processAction($this->request, 'edit', $id);
      }
    }
  }

  private function _processAction($dataIn, $action='add', $id=null) {
      $data = $this->_processRequestData($dataIn);
      $this->Article->set($data);
      if ($this->Article->validates()) {
        if (!empty($data["Article"]["image_path"]) && !empty($data["Article"]["image_path"]['name'])) {
          $imageUploaded = $this->Upload->uploadImage($data["Article"]["image_path"], WWW_ROOT . '/' . Configure::read('App.imageFolder') . '/' .Configure::read('App.imageUploadFolder'), Configure::read('App.articlesImageWidth'));
          $data["Article"]["image_path"] = Configure::read('App.imageUploadFolder') . '/' . $imageUploaded;
        } else {
          if($action==='add') {
            $data["Article"]["image_path"] = null;
          } else {
            $data["Article"]["image_path"] = isset($data["Article"]["_image_path"]) ? $data["Article"]["_image_path"] : null;
          }
        }
        if ($this->Article->save($data, false)) {
          $this->Session->setFlash('The record was successfully saved.');
          $this->redirect(array('action' => 'index'));
        }
      } else {
        if($action!=='add') {
          //getting previous image
          $data['Article']['image_path']=$data["Article"]["_image_path"];
          $data['Article']['id']=$id;
        }
        $this->request->data['Article'] = $data['Article'];
        $this->set('errors', $this->Article->validationErrors);
      }
      if($action==='add') {
        $this->render('admin_add');
      } else {
        $this->render('admin_edit');
      }
  }
  
  function admin_delete($id) {
    $this->layout = "admin";
    $this->admin_deleteImage($id, false);
    //http://book.cakephp.org/2.0/en/models/deleting-data.html
    if ($this->Article->delete($id, true)) {
      $this->Session->setFlash('The record has been deleted.');
      $this->redirect(array('action' => 'index'));
    }
  }

  //admin
  function admin_deleteImage($id, $redirect=true) {
    $image = $this->Article->read('image_path', $id);
    $imagePath = $image['Article']['image_path'];
    if (!empty($imagePath)) {
      $result = $this->Util->deleteFile(WWW_ROOT . '/' . Configure::read('App.imageFolder') . '/' . $imagePath);
      $this->Article->save(array('Article' => array('id' => $id, 'image_path' => '')), false, array('image_path'));
      if($redirect){
        $this->Session->setFlash('The image has been deleted.');
        $this->redirect(array('action' => 'edit', $id));
      }
    }
  }

}

?>
